package com.it.blog.controller;

import com.github.pagehelper.PageInfo;
import com.it.base.utils.R;
import com.it.blog.domain.Article;
import com.it.blog.domain.ArticleType;
import com.it.blog.dto.PlaceOnFileDTO;
import com.it.blog.query.ArticleQuery;
import com.it.blog.service.ArticleTypeService;
import com.it.blog.service.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController//@rc
@RequestMapping("/article")
@Api(tags = "文章接口类")
public class ArticleController {
    @Autowired
    private IArticleService articleService;
    @Autowired
    private ArticleTypeService articleTypeService;

    @GetMapping
    @ApiOperation(value = "查询所有文章")
    public R<List<Article>> findAll() {
        List<Article> articles = articleService.findAll();
        System.out.println(articles);
        return R.ok(articles);//alt enter生成返回值
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个文章")
    public R<Article> findOne(@PathVariable Long id) {
        Article article = articleService.findOne(id);
        return R.ok(article);//alt enter生成返回值
    }

    @PostMapping
    @ApiOperation(value = "分页+条件查询文章")
    public PageInfo<Article> queryPage(@RequestBody ArticleQuery articleQuery) {
        List<Article> list = articleService.queryPage(articleQuery);
        return new PageInfo<Article>(list);//alt enter生成返回值,list分页集合
    }

    @PutMapping
    @ApiOperation(value = "增加or修改文章")
    public R<Void> addOrUpdate(@RequestBody Article article) {
        //try
        try {
            Long id = article.getId();

            if (id==null){
                articleService.addArticle(article);
            }else {
                articleService.updateArticle(article);
            }
        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除单个文章")
    public R<Void> deleteArticle(@PathVariable Long id) {
        //try
        try {
            articleService.deleteArticle(id);
        } catch(Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @PatchMapping
    @ApiOperation(value = "文章批量删除")
    public R<Void> batchArticles(@RequestBody Long[] ids) {
        try {
            articleService.batchArticles(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }

        return R.ok();
    }

    @GetMapping("/getTypeTree/{id}")
    @ApiOperation("查询文章类型树")
    public R<List<ArticleType>> getTypeTree(@PathVariable Long id) {
        List<ArticleType> articleTypeList=articleService.getTypeTree(id);
        return R.ok(articleTypeList);
    }

    @GetMapping("/articleTimeStatistics")
    @ApiOperation("归档博客文章")
    public R<List<PlaceOnFileDTO>> getArticleTimeStatistics() {
        List<PlaceOnFileDTO> articleTimeStatistics=articleService.getArticleTimeStatistics();
        return R.ok(articleTimeStatistics);
    }

    @GetMapping("/getRelatedArticles/{id}")
    @ApiOperation("相关文章查询")
    public R<List<Article>> getRelatedArticles(@PathVariable Long id) {
        List<Article> articleList=articleService.getRelatedArticles(id);
        return R.ok(articleList);
    }

    @GetMapping("/getArchiveDetail/{yearMonth}")
    public R<List<Article>> getArchiveDetail(@PathVariable String yearMonth) {
        List<Article> articleList=articleService.getArchiveDetail(yearMonth);
        return R.ok(articleList);
    }

    @GetMapping("/findAllArticle")
    public R<List<Article>> findAllArticle() {
        List<Article> articles = articleService.findAllArticle();
        System.out.println(articles);
        return R.ok(articles);//alt enter生成返回值
    }
}
